package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.AnnualReportRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.TransferHistoryRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.UnitArchiveManageRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.TransferHistory;
import com.ktwlsoft.archivemanage.payload.transfer.*;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TransferService {

    private final UnitArchiveManageRepositoryService unitArchiveManageRepositoryService;

    private final AnnualReportRepositoryService annualReportRepositoryService;

    private final TransferHistoryRepositoryService transferHistoryRepositoryService;

    public TransferService(UnitArchiveManageRepositoryService unitArchiveManageRepositoryService, AnnualReportRepositoryService annualReportRepositoryService, TransferHistoryRepositoryService transferHistoryRepositoryService) {
        this.unitArchiveManageRepositoryService = unitArchiveManageRepositoryService;
        this.annualReportRepositoryService = annualReportRepositoryService;
        this.transferHistoryRepositoryService = transferHistoryRepositoryService;
    }

    public PageInfo<TransferListResponse> findByPage(int pn, int pageSize, String keywords, Boolean transfered){
        PageHelper.startPage(pn, pageSize);
        return new PageInfo<>(unitArchiveManageRepositoryService.findTransferListByPage(keywords, transfered));
    }


    public TransferDetail findDetail(Long unit) {
        return unitArchiveManageRepositoryService.findTransferDetailById(unit);
    }

    public PageInfo<TransferDetailList> findDetailList(int pn, int pageSize, Long unitId, String field, String order) {
        PageHelper.startPage(pn,pageSize*7);
        return new PageInfo<>(unitArchiveManageRepositoryService.findTransferDetailList(unitId, field, order));
    }

    @Transactional
    public boolean transferArchives(ArchivesTransferRequest request) {
        if(request.getBusinessFrom().equals("manual")){
            unitArchiveManageRepositoryService.setTransfered(request.getBusinessId(),
                    UserPrincipal.getCurrentUser().getRealName(),
                    UserPrincipal.getCurrentUser().getId(),
                    request.getTransferRemark(),
                    new Date());
        }else{
            annualReportRepositoryService.setTransfered(request.getBusinessId(),
                    UserPrincipal.getCurrentUser().getRealName(),
                    UserPrincipal.getCurrentUser().getId(),
                    request.getTransferRemark(),
                    new Date());
        }
        TransferHistory transferHistory = new TransferHistory(
                request.getBusinessId(),
                request.getBusinessFrom(),
                new Date(),
                request.getTransferRecord());
        transferHistoryRepositoryService.insert(transferHistory);
        return true;
    }

    public String showDiff(Long businessId) {
        return transferHistoryRepositoryService.getByBusinessId(businessId);
    }

    @Transactional
    public boolean cancelTransfer(CacnelTransferRequest request) {
        transferHistoryRepositoryService.delete(request.getBusinessId());
        if(request.getBusinessFrom().equals("manual")){
            unitArchiveManageRepositoryService.cancelTransfer(
                    request.getBusinessId(),
                    UserPrincipal.getCurrentUser().getRealName(),
                    UserPrincipal.getCurrentUser().getId(),
                    request.getTransferRemark(),
                    new Date());
        }else{
            annualReportRepositoryService.cancelTransfer(
                    request.getBusinessId(),
                    UserPrincipal.getCurrentUser().getRealName(),
                    UserPrincipal.getCurrentUser().getId(),
                    request.getTransferRemark(),
                    new Date()
            );
        }
        return true;
    }
}
